package com.auth;

import javax.imageio.ImageIO;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.ServletOutputStream;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.Random;
//2020.11.12
public class checkCode {
    private static final char chars[]={'2','3','4','5','6','7','8','A','C',
            'D','E','F','G','H','J','K','M','N','P','Q',
            'R','S','T','U','V','W','X','Y','Z'}; //code字符源
    private static final Color colors[]={Color.orange,Color.white,Color.pink,Color.red};//颜色源
    private static Random random=new Random();
    private static String format="jpg";
    private static final String dirPath=System.getProperty("user.dir");
    private static final String filePath=dirPath+"\\web\\code111.jpg";
    private static final int width=80;
    private static final int height=35;
    private static final int imageType=BufferedImage.TYPE_INT_RGB;
    private static final int codeX=15;
    private static final int codeY=20;


    public static BufferedImage getImage(int width,int height,int imageType){
        return new BufferedImage(width,height,imageType);
    }

    public static Graphics getGraphics(BufferedImage img){
        return img.getGraphics();
    }

    //get验证码
    public static String getCheckCode(char[] chars){

        StringBuilder stringBuilder=new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int index = random.nextInt(29);
            stringBuilder.append(chars[index]);
        }
        return stringBuilder.toString();
    }

    public static void setFontColor(Graphics graphics){
        int i = random.nextInt(4);
        graphics.setColor(colors[i]);
    }

    public static void outputImage(BufferedImage image, String format, OutputStream os){
        try {
            ImageIO.write(image,format,os);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void drawString(String checkCode ,Graphics graphics){
        if(graphics!=null){

             graphics.drawString(checkCode,codeX,codeY);
        }
    }
    //干扰线
    public static void drawLine(Graphics graphics){
        for (int i = 0; i < 3; i++) {
            int x1 = random.nextInt(50)+5;
            int y1 = random.nextInt(22)+5;
            graphics.drawLine(x1,y1,x1+16,y1+11);
        }


    }

    public  static void makeDir(String dirPath){
        File file=new File(dirPath);
        if(!file.exists()){
            file.mkdir();
        }
    }

    public static char[] getChars() {
        return chars;
    }

    public static Color[] getColors() {
        return colors;
    }

    public static Random getRandom() {
        return random;
    }

    public static void setRandom(Random random) {
        checkCode.random = random;
    }

    public static String getFormat() {
        return format;
    }

    public static void setFormat(String format) {
        checkCode.format = format;
    }

    public static String getDirPath() {
        return dirPath;
    }

    public static String getFilePath() {
        return filePath;
    }

    public static int getWidth() {
        return width;
    }

    public static int getHeight() {
        return height;
    }

    public static int getImageType() {
        return imageType;
    }

    public static int getCodeX() {
        return codeX;
    }

    public static int getCodeY() {
        return codeY;
    }

    //    /**
//     * 测试
//     * @param args
//     */
//    public static void main(String[] args) throws IOException {
//        BufferedImage img = getImage(width, height, imageType);
//
//        Graphics graphics = getGraphics(img);
//
//
//
//        String code = getCheckCode(chars); //因为这里的checkCode是大写的所以在用户输入需要转换
//
//        setFontColor(graphics);
//
//        drawString(code,graphics);
//
//        drawLine(graphics);
//
//        makeDir(dirPath);
//
//        outputImage(img,format,new FileImageOutputStream(new File(filePath)));
//
//
//
//
//    }



}
